/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import database.CocktailTable;
import database.IngredientTable;
import database.RecipeTable;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Cocktail;
import model.Ingredient;
import model.Recipe;

/**
 *
 * @author tamara
 */
public class NewCocktailServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        CocktailTable cocktailtable = new CocktailTable();
        IngredientTable ingredienttable = new IngredientTable();
        RecipeTable recipetable = new RecipeTable();
        ArrayList<Ingredient> ingredients = ingredienttable.getIngredients();

        String eingabe = request.getParameter("btSubmit");

        if (eingabe.equals("New Cocktail")) {
            request.setAttribute("ingredients", ingredients);
            RequestDispatcher rd = request.getRequestDispatcher("/newCocktail.jsp");
            rd.forward(request, response);
        }

        if (eingabe.equals("AddNewCocktail")) {
            String cocktailname = request.getParameter("cocktailname");
            String ingredient1 = request.getParameter("cb_zutat1");
            String ingredient2 = request.getParameter("cb_zutat2");
            String ingredient3 = request.getParameter("cb_zutat3");
            String menge1 = request.getParameter("menge1");
            String menge2 = request.getParameter("menge2");
            String menge3 = request.getParameter("menge3");
            String einheit1 = request.getParameter("einheit1");
            String einheit2 = request.getParameter("einheit2");
            String einheit3 = request.getParameter("einheit3");

            Recipe recipe;
            String ingredientname;
            String ingredientID;
            String[] both = ingredient1.split(" ");
            ingredientname = both[1];
            ingredientID = both[0];

            if (!cocktailname.equals("")) {
                Cocktail cocktail = cocktailtable.newCocktail(cocktailname);

                if (!menge1.equals("")) {
                    recipe = new Recipe(cocktail.getCocktailID(), Integer.parseInt(ingredientID), Integer.parseInt(menge1), einheit1, cocktail.getCocktailname(), ingredientname);
                    recipetable.newRecipe(cocktail, recipe);
                }

                if (!menge2.equals("")) {
                    both = ingredient2.split(" ");
                    ingredientname = both[1];
                    ingredientID = both[0];
                    recipe = new Recipe(cocktail.getCocktailID(), Integer.parseInt(ingredientID), Integer.parseInt(menge2), einheit2, cocktail.getCocktailname(), ingredientname);
                    recipetable.newRecipe(cocktail, recipe);
                }

                if (!menge3.equals("")) {
                    both = ingredient3.split(" ");
                    ingredientname = both[1];
                    ingredientID = both[0];
                    recipe = new Recipe(cocktail.getCocktailID(), Integer.parseInt(ingredientID), Integer.parseInt(menge3), einheit3, cocktail.getCocktailname(), ingredientname);
                    recipetable.newRecipe(cocktail, recipe);
                }
                RequestDispatcher rd = request.getRequestDispatcher("/cocktailAdded.jsp");
                rd.forward(request, response);
            } else {

                RequestDispatcher rd = request.getRequestDispatcher("/cocktaillnotAdded.jsp");
                rd.forward(request, response);
            }
        }

        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet NewCocktailServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet NewCocktailServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
